﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frw.DAL;

namespace Frw
{
    public class FacadeBase<I, C, CTX> : BusinessClassBase<I, C>
        where C : class, I
        where I : class
        where CTX : class, new()
    {
        private static Dictionary<string, object> m_cacheContext = new Dictionary<string,object>();

        public CTX GetContext() 
        {
            CTX retValue = default(CTX);

            if(!m_cacheContext.Keys.Contains(typeof(CTX).Name))
            {
                m_cacheContext.Add(typeof(CTX).Name, retValue = new CTX());
            }
            else
            {
                retValue = m_cacheContext[typeof(CTX).Name] as CTX;
            }

            return retValue; 
        }

        public FacadeBase()
        {
        }

        //public static I GetInstance()
        //{
        //    return GenericClassFactory<I, C>.CreateInstance();
        //}

        //public static I GetInstance(params object[] args)
        //{
        //    return GenericClassFactory<I, C>.CreateInstance(args);
        //}
    }

    public class FacadeBase<I, C> : BusinessClassBase<I, C>
        where C : class, I
        where I : class
    {
        private static Dictionary<string, object> m_cacheContext = new Dictionary<string, object>();

        public object GetContext()
        {
            object retValue = default(object);

            Type m_context = null;
            if (this.GetType().GetCustomAttributes(true).Length != 1)
            {
                throw new ApplicationException("Context Attribute Missing !");
            }
            else
            {
                m_context = ((ContextAttribute)this.GetType().GetCustomAttributes(true)[0]).Context;
            }

            if (m_context == null)
            {
                throw new ApplicationException("Data Context  Missing !");
            }

            if (!m_cacheContext.Keys.Contains(m_context.Name))
            {
                m_cacheContext.Add(m_context.Name, retValue = Activator.CreateInstance(m_context));
            }
            else
            {
                retValue = m_cacheContext[m_context.Name] as object;
            }

            return retValue;
        }

        public FacadeBase()
        {
        }
    }
}
